package com.carlwang.web.comtroller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.Controller;

import com.carlwang.pojo.MyTask;
import com.carlwang.pojo.TaskList;
import com.carlwang.service.TaskDaoService;
import com.task.MyTimerTask;

@org.springframework.stereotype.Controller("/hello")
public class HelloWorldController {
	@Resource(name="taskDaoServiceImp")
	private TaskDaoService mTaskDaoService;
	private TaskList mTaskList;
	private MyTimerTask mCurrentTask;

	@RequestMapping("/helloWorld")
	public String helloWorld(Model model,
			@ModelAttribute("saveTask") TaskList mTaskList) {
		System.out.println(model);
		model.addAttribute("message", "Hello World!");
		System.out.println("helloWorld");
		return "hello";
	}

	@RequestMapping("/playMusic")
	public void playMusic(TaskList taskList, MyTask myTask) {
		
		System.out.println(myTask);
		String currentDate = myTask.getStartTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		Date date = null;
		try {
			date = sdf.parse(currentDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		MyTimerTask task = getCurrentTask();
		Timer timer = new Timer();
		if (date == null) {
			return;
		}
		timer.schedule(task, date);

	}



	public TaskList getTaskList() {
		return mTaskList;
	}

	public void setTaskList(TaskList taskList) {
		mTaskList = taskList;
	}

	public TaskDaoService getTaskDaoService() {
		return mTaskDaoService;
	}

	public void setTaskDaoService(TaskDaoService taskDaoService) {
		mTaskDaoService = taskDaoService;
	}

	public MyTimerTask getCurrentTask() {
		return mCurrentTask;
	}

	public void setCurrentTask(MyTimerTask currentTask) {
		mCurrentTask = currentTask;
	}

}
